package com.sg.business.model.dataset.projectgroup;

import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sg.business.model.ILifecycle;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.Project;
import com.sg.business.model.ProjectGroup;
import com.sg.widgets.commons.dataset.MasterDetailDataSetFactory;

public class ProjectSelectorOfProjectGroup extends MasterDetailDataSetFactory {

	public ProjectSelectorOfProjectGroup() {
		super(IModelConstants.DB, IModelConstants.C_PROJECT);
	}

	@Override
	protected String getDetailCollectionKey() {
		return Project.F__ID;
	}

	@Override
	public DBObject getQueryCondition() {
		if (master instanceof ProjectGroup) {
			List<?> projectIds = ((ProjectGroup) master).getProjectIds();
			DBObject dbo = new BasicDBObject();
			if (projectIds != null) {
				dbo.put(Project.F__ID,
						new BasicDBObject().append("$nin", projectIds));
			}
			dbo.put(Project.F_LIFECYCLE,
					new BasicDBObject().append("$nin", new String[] {
							ILifecycle.STATUS_FINIHED_VALUE,
							ILifecycle.STATUS_CANCELED_VALUE }));
			dbo.put(Project.F_FUNCTION_ORGANIZATION,
					((ProjectGroup) master).getFunctionOrganizationId());
			return dbo;
		}
		return super.getQueryCondition();
	}
}
